package cn.com.callback

import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONArray
import org.json.JSONObject

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: 获取录音接口*
 * @Description: 通过用户手机号/用户编号/通话ID获取对应的录音记录
 * @Author: admin*
 * @Date 2022/8/18 16:26
 *
 */
class apiGetCallRecord implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiGetCallRecord.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        long startTime = System.currentTimeMillis();

        //初始化返回参数
        String Syncpram = "";
        JSONObject result = new JSONObject();
        JSONArray  jsonArr=new JSONArray()
        long endTime=0;

        //判断调用方式获取接口入参
        String getMethod = request.getMethod();
        if (getMethod.equals("GET")) {
            Syncpram = request.getQueryString();
            log.info("接收调用GET参数信息：" + Syncpram);
        } else if (getMethod.equals("POST")) {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                Syncpram += line;
            }
            log.info("接收调用POST报文信息：" + Syncpram);
        }
        //获取参数业务逻辑处理
        try {
            JSONObject inObject = new JSONObject(Syncpram);
            log.debug("处理前的Json：" + inObject.toString());

            String timestamp = KHUtil.GetJV(inObject, "timestamp");//时间戳，精确到秒
            String sign = KHUtil.GetJV(inObject, "sign");//签名
            String mobile = KHUtil.GetJV(inObject, "mobile");//用户手机号(用户手机号/用户编号/通话ID3个必须有一个)
            String userno = KHUtil.GetJV(inObject, "userno");//用户编号(用户手机号/用户编号/通话ID3个必须有一个)
            String callid = KHUtil.GetJV(inObject, "callid");//通话ID(用户手机号/用户编号/通话ID3个必须有一个)

            String apiid="waterOff";
            String apikey="55bd38420d354822bdb789284fac4ab3";
            String signFront="apiid"+"="+apiid+"&"+"apikey"+"="+apikey+"&"+"timestamp"+"="+timestamp;
            log.debug("sign加密Md5前"+signFront);
            String signafter= Util.getMD5Str(signFront);
            log.debug("sign加密Md5后"+signafter);

            //签名校验
            if(!sign.equals(signafter)){
                log.debug("signafter不存在")
                endTime=System.currentTimeMillis();
                result.put("code", "-1");
                result.put("msg", "调用失败:签名错误，调用处理时长(毫秒)" + (endTime-startTime));
                result.put("data", jsonArr);
                return result.toString();
            }

            //必填校验
            if (KHUtil.isNull(mobile)&&KHUtil.isNull(userno)&&KHUtil.isNull(callid)) {
                endTime=System.currentTimeMillis();
                result.put("code", "-1");
                result.put("msg", "调用失败:用户手机号(用户手机号/用户编号/通话ID3不能全部为空，调用处理时长(毫秒)" +  (endTime-startTime));
                result.put("data", jsonArr);
                return result.toString();
            }

            //拼接查询条件
            String queryConditions="";
            if(!KHUtil.isNull(mobile)){
                //手机号
                queryConditions="and (ani='"+mobile+"' or dnis='"+mobile+"')";
            }else if(!KHUtil.isNull(userno)){
                //用户编号
                queryConditions="and c.customer_id='"+userno+"'";
            }else if(!KHUtil.isNull(callid)){
                //通话ID
                queryConditions="and r.record_guid='"+callid+"'";
            }

            String recordUrl= KHDBConfig.getAPIURL(eliteBean,"RECORD-URL");

            String getRecordUrlSql="select r.record_guid,r.starttime,r.endtime,r.documentpath from record r left join customer c on r.customer_guid=c.customer_guid where 1=1 "+queryConditions;
            List recordList=eliteBean.chaxun_sql(getRecordUrlSql,"dataSource");
            for(int i=0;i<recordList.size();i++){
                JSONObject jsonObj=new JSONObject();
                String record_guid=recordList.get(i).get("record_guid");
                String callstarttime=recordList.get(i).get("starttime");
                String callendtime=recordList.get(i).get("endtime");
                String documentpath=recordList.get(i).get("documentpath");
                jsonObj.put("callid",record_guid);
                jsonObj.put("starttime",callstarttime);
                jsonObj.put("endtime",callendtime);
                jsonObj.put("documentpath",recordUrl+record_guid+"&queryType=record_guid");
                jsonArr.put(jsonObj);
            }

             endTime = System.currentTimeMillis();
            long time = (endTime - startTime)

            result.put("code", "1");
            result.put("msg","录音获取完成,调用处理时长(毫秒)"+time);
            result.put("data", jsonArr);

        } catch (Exception e) {
            log.error("处理异常!", e);
            result.put("code", "-1");
            result.put("msg", e.printStackTrace());
            result.put("data", jsonArr);
        }

    }
}
